package com.qf.business.message.handler;

import com.alibaba.fastjson.JSON;
import com.qf.business.message.entity.Message;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

/**
 * 出站的消息处理器 将Message对象转换成一个文本帧
 * author Ken
 * create_time 2023/1/4
 */
public class TextOutChannelHandler extends ChannelOutboundHandlerAdapter {

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        if (msg instanceof Message) {
            msg = new TextWebSocketFrame(JSON.toJSONString(msg));
        }
        super.write(ctx, msg, promise);
    }
}
